package com.health.trans.kit;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.health.trans.enums.SqlCondition;
import com.health.trans.pojo.FieldInfo;
import com.health.trans.pojo.TableInfo;
import com.health.trans.pojo.TransParam;

import java.util.List;

/**
 * @author nqlz
 */
public class BatchConditionKit {

    static final String WHERE = "where";


    public static String appendSql(StringBuilder sb, String sql) {
        if (sb.toString().contains(WHERE)) {
            sb.append(" and ").append(sql);
        } else{
            sb.append(" where ").append(sql);
        }
        return sb.toString();
    }

    static void generate(StringBuilder sb, String column, Object obj, String condition) {
        sb.append(column).append(" ").append(condition).append(" '").append(obj).append("'");
    }

    /**
     * 获得sql str
     *
     * @param tableInfo  表信息
     * @param transParam 反式参数
     * @return {@link StringBuilder}
     */
    public static <T> StringBuilder getSqlStr(TableInfo<T> tableInfo, TransParam transParam) {
        StringBuilder whereSql = new StringBuilder();
        List<FieldInfo> query = tableInfo.getFieldInfos();
        if (CollUtil.isNotEmpty(query)) {
           query.forEach(i-> appendSql(whereSql,fieldInfoCondition(i)));
        }

        String hospCode = transParam.getReader().getHospCode();
        if (StrUtil.isNotBlank(hospCode) && !whereSql.toString().contains("hosp_code")) {
            String hospSql = getFinalSql(null, hospCode, "hosp_code",  SqlCondition.eq);
            appendSql(whereSql,hospSql);
        }

        return whereSql;
    }


    private static String fieldInfoCondition(FieldInfo a) {
        Object value = a.getValue();
        SqlCondition condition = a.getCondition();
        return getFinalSql(a, value, a.getDbName(), condition);
    }

    private static String getFinalSql(FieldInfo a, Object value, String fieldName, SqlCondition condition) {
        StringBuilder sql = new StringBuilder();
        switch (condition) {
            case gt: {
                generate(sql, fieldName, value, ">");
                break;
            }
            case gte: {
                generate(sql, fieldName, value, ">=");
                break;
            }
            case in: {
                sql.append(fieldName).append("in(").append(value.toString()).append(")");
                break;
            }
            case notIn: {
                sql.append(fieldName).append(" not in(").append(value.toString()).append(")");
                break;
            }
            case lt: {
                generate(sql, fieldName, value, "<");
                break;
            }
            case lte: {
                generate(sql, fieldName, value, "<=");
                break;
            }
            case like: {
                sql.append(fieldName).append(" like ").append("%").append(value).append("%");
                break;
            }
            case leftLike: {
                sql.append(fieldName).append(" like ").append("%").append(value);
                break;
            }
            case rightLike: {
                sql.append(fieldName).append(" like ").append(value).append("%");
                break;
            }
            case notLike: {
                sql.append(fieldName).append(" not like ").append("%").append(value).append("%");
                break;
            }
            case between: {
                sql.append(fieldName).append(" between '").append(DateUtil.formatDateTime(a.getStartTime()))
                        .append("' and '").append(DateUtil.formatDateTime(a.getEndTime()))
                        .append("')");
                break;
            }
            case isNull: {
                sql.append(fieldName).append("  is NULL");
                break;
            }
            case notNull: {
                sql.append(fieldName).append(" IS NOT NULL");
                break;
            }
            case ne: {
                sql.append(fieldName).append(" <> '").append(value).append("'");
                break;
            }
            default: {
                sql.append(fieldName).append(" = '").append(value).append("'");
                break;
            }
        }
        return sql.toString();
    }




}
